package test.streaming;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import test.MyFlatMapper;

/**
 * Stream流处理WordCount
 */
public class WordCount2 {

    public static void main(String[] args) throws Exception {
        //创建流处理执行环境（Flink上下文）
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //env.setParallelism(1);//并行度设置
        //从文件中读取数据
        String path1 = "D:\\IdeaProjects\\springboot-flink-1\\flinkTutorial\\src\\main\\resources\\hello.txt";
        DataStreamSource<String> inputDataStream = env.readTextFile(path1);
        //基于数据流进行转换计算
        SingleOutputStreamOperator<Tuple2<String, Integer>> resultStream = inputDataStream.flatMap(new MyFlatMapper())
                .keyBy(0)
                .sum(1);

        resultStream.print();

        //执行任务
        env.execute();

    }
}
